<?php
class AsignacionController extends Controller
{   var $widKey = false;
    
    public function actionGridLoadData(){
        $cont = 0;
        $data = '';
        $model = tbl_asignacion::model()->findAll();        
        foreach($model as $value){
            $cont++;
            $municipio = tbl_municipio::model()->find('mun_id=:keyMun', array(':keyMun' => $value->asig_mun));
            $value->asig_bole == 'BINF'?$bolDetalle = 'Boleta de Infraestructura':$bolDetalle = 'Boleta de Gestión';
            $data[] = array('id'=>$cont,'data'=>array(
                $cont,
                $value->asig_mun,
                $municipio->mun_detalle,
                $bolDetalle,
                implode('-',array_reverse(explode('-',$value->asig_fechaini))),
                implode('-',array_reverse(explode('-',$value->asig_fechafin)))
            ));            
        }
        $allData = array('rows'=>$data);
        echo 'data = '.CJSON::encode($allData);  
    }
    public function actionSave_data()
    {                                              
        $data = explode(',', $_REQUEST['listMun']);
        if($_REQUEST['tip_boleta'] == 'ALL'){
            $boleta = array('BINF', 'BGES');
            foreach($boleta as $bolValue){                    
                foreach ($data as $valueMun){
                    $model = new tbl_asignacion();
                    $transaction = $model->dbConnection->beginTransaction();
                    try{
                        $model->asig_mun = $valueMun;
                        $model->asig_bole = $bolValue; 
                        $model->asig_fechaini = $_REQUEST['fecha_ini'];
                        $model->asig_fechafin = $_REQUEST['fecha_fin'];
                        $model->save();
                        $transaction->commit();                        
                    } catch(Exception $e) {
                        $transaction->rollback();
                        echo "Error system controller 'Asignacion' in the action -> Add.".count($_POST);           
                    }
                }
            }
        }
        else{
            foreach ($data as $valueMun){
                $model = new tbl_asignacion();
                $transaction = $model->dbConnection->beginTransaction();
                try{
                    $model->asig_mun = $valueMun;
                    $model->asig_bole = $_REQUEST['tip_boleta'];
                    $model->asig_fechaini = $_REQUEST['fecha_ini'];
                    $model->asig_fechafin = $_REQUEST['fecha_fin'];
                    $model->save();
                    $transaction->commit();                    
                } catch(Exception $e) {
                    $transaction->rollback();
                    echo "Error system controller 'Asignacion' in the action -> Add.".count($_POST);            
                }
            }
        }
    }
    public function actionEdit_data()
    {                
        $command = Yii::app()->db->createCommand();
        $command->update('tbl_asignacion', array(
            'asig_bole'=>$_REQUEST['tip_boleta'],
            'asig_fechaini'=>$_REQUEST['fecha_ini'],
            'asig_fechafin'=>$_REQUEST['fecha_fin'],
        ), 'asig_mun=:keyMun AND asig_bole=:keyBol', array(':keyMun'=>$_REQUEST['listMun'], ':keyBol'=>$_REQUEST['tip_boleta']));                
    }
    public function actionErase_data()
    {        
        $command = Yii::app()->db->createCommand();
        $command->delete('tbl_asignacion', 'asig_mun=:keyMun AND asig_bole=:keyBol', array(':keyMun'=>$_REQUEST['id'], ':keyBol'=>$_REQUEST['bol']));                
        echo true;
    }
}
